﻿/* 功能:  个人商户管理
 * 创建人：Kencery  创建时间：2018-2-2
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.merchantuser = (function () {
    var defaults = {
        listUrl: "/Merchant/ListUser",
        uploadUrl: "/Common/UploadImage",
        getPlatFormInfoUrl: "/PlatFormCategory/GetDropDownListList",
        addUrl: "/Merchant/Add",
        editUrl: "/Merchant/Edit",
        businessUrl: "/Merchant/Business",
        detailUrl: "/Merchant/GetDetailById",
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '商户ID',
        field: 'Id',
    }, {
        title: '平台分类名称',
        field: 'PlatFormName',
        align: 'center',
    }, {
        title: '商户名',
        field: 'Name',
        align: 'center',
    }, {
        title: '商户Logo',
        field: 'Logo',
        align: 'center',
        formatter: function (value, rows, index) {
            return '<div><img src="' + value + '" style="width:40px;height:40px;"></div>';
        }
    }, {
        title: '电话',
        field: 'Phone',
        align: 'center',
    }, {
        title: '营业开始时间',
        field: 'BusinessStartHours',
        align: 'center',
    }, {
        title: '结束时间',
        field: 'BusinessEndHours',
        align: 'center',
    }, {
        title: '配送费',
        field: 'DistributionFee',
        align: 'center',
    }, {
        title: '营业',
        field: 'JoinMark',
        align: 'center',
        formatter: function (value, rows, index) {
            var result = returnJoinMark(value);
            return '<div id="State_' + rows.Id + '">' + result + '</div>';
        }
    }, {
        title: '创建',
        field: 'CreateTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#merchantuserinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    }

    var queryParams = function (params) {
        return {
            offset: params.offset,             //后台计算显示数据信息
            pagesize: params.limit,            //每页显示多少行
        };
    };

    var initDropDownList = function () {
        plscommon.dropDownList("#PlatFormId", defaults.getPlatFormInfoUrl, 1, 1);
    }

    var clickEvent = function () {
        $("#file").on("change", function (e) { btnUploadFile(e) });                                     //上传图片
        $("#btnAddMerchantDialog").on("click", function () { btnAddMerchantDialog(); });                //添加商户弹出层
        $("#btnUpdateMerchantDialog").on("click", function () { btnUpdateMerchantDialog(); });          //修改商户弹出层
        $("#btnAddMerchantOK").on("click", function () { btnAddMerchantOK(); });                        //添加修改商户信息

        $("#btnYesJoinMarkDialog").on("click", function () { btnYesJoinMarkDialog(); });                //营业弹出层
        $("#btnNoJoinMarkDialog").on("click", function () { btnNoJoinMarkDialog(); });                  //休息弹出层
        $("#btnBusiness").on("click", function () { btnBusiness(); });                                  //营业休息事件

        $("#btnDetailDialog").on("click", function () { btnDetailDialog(); });                          //订单详情
    };

    var btnUploadFile = function (e) {
        var file = e.target.files[0] || e.dataTransfer.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function () { }
            reader.readAsDataURL(file);
        }
        var path = plscommon.uploadFile("post", defaults.uploadUrl, document.getElementById("file").files[0]);
        $("#Logo").attr("src", path);
    }

    var btnAddMerchantDialog = function () {
        //首先JS中判断是否含有数据，如果含有数据不允许在添加
        var total = $('#merchantuserinfo').bootstrapTable('getOptions').totalRows;
        if (total > 0) {
            plscommon.warningMessage("您已经含有一个店铺了，不允许在添加店铺", 3000);
            return false;
        }

        $('#AddMerchantModal').modal();
        plscommon.resetFrom("merchantOperation");
        $("#HeadTitle").text("添加商户");
        opt.type = 1;
        $("#showUserImage").attr("src", "");
    }

    var btnUpdateMerchantDialog = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('merchantuserinfo', 'AddMerchantModal', 'merchantOperation');
        if (!data.Id) {
            return false;
        }

        $('#AddMerchantModal').modal();
        plscommon.resetFrom("merchantOperation");
        $("#HeadTitle").text("修改商户");
        opt.type = 2;

        //赋值给控件信息(包括图片展示)
        $("#Id").val(data.Id);
        $("#Name").val(data.Name);
        $("#PlatFormId").selectpicker("val", data.PlatFormId);
        $("#Notice").val(data.Notice);
        $("#Phone").val(data.Phone);
        $("#Address").val(data.Address);
        $("#BusinessStartHours").val(data.BusinessStartHours);
        $("#BusinessEndHours").val(data.BusinessEndHours);
        $("#DistributionFee").val(data.DistributionFee);
        $("#Logo").attr("src", data.Logo);
    }

    var btnAddMerchantOK = function () {
        //读取form表单中的值并且进行验证
        var data = plscommon.deleteByValue($("#merchantOperation").serializeArray(), "Id");
        data = plscommon.transitionArray(data);
        data.Logo = $("#Logo").attr("src");
        if (!checkData(data)) {
            return false;
        }

        $("#btnAddMerchantOK").prop("disabled", true);
        if (opt.type == 1) {
            plscommon.ajax_status({
                disableId: "btnAddMerchantOK",
                url: defaults.addUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddMerchantModal').modal('hide');
                        plscommon.refreshTable("merchantuserinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        } else {
            data.id = $("#Id").val();
            plscommon.ajax_status({
                disableId: "btnAddMerchantOK",
                url: defaults.editUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddMerchantModal').modal('hide');
                        plscommon.refreshTable("merchantuserinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        }
    }

    var btnYesJoinMarkDialog = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('merchantuserinfo', 'BusinessModal', 'businessOperation');
        if (!data.Id) {
            return false;
        }

        //营业
        $('#BusinessModal').modal();
        plscommon.resetFrom("businessOperation");
        $("#merchant_id").val(data.Id);
        $("#merchantName").text(data.Name);
        $("#merchantName_OK").text("营业该商户(" + data.Name + ")");
        opt.JoinMark = 1;
    }

    var btnNoJoinMarkDialog = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('merchantuserinfo', 'BusinessModal', 'businessOperation');
        if (!data.Id) {
            return false;
        }

        //休息
        $('#BusinessModal').modal();
        plscommon.resetFrom("businessOperation");
        $("#merchant_id").val(data.Id);
        $("#merchantName").text(data.Name);
        $("#merchantName_OK").text("休息该商户(" + data.Name + ")");
        opt.JoinMark = 0;
    }

    var btnBusiness = function () {
        var data = plscommon.dialogWaring('merchantuserinfo', 'BusinessModal', 'businessOperation');
        if (opt.JoinMark == 0 && data.JoinMark == 0) {  //未营业
            plscommon.warningMessage("该商户已经是未营业状态，不允许在修改", 3000);
            return false;
        }
        if (opt.JoinMark == 1 && data.JoinMark == 1) {  //营业
            plscommon.warningMessage("该商户已经是营业状态，不允许在修改", 3000);
            return false;
        }

        $("#btnBusiness").prop("disabled", true);
        plscommon.ajax_status({
            disableId: "btnBusiness",
            url: defaults.businessUrl,
            type: "POST",
            data: { Id: $("#merchant_id").val(), JoinMark: opt.JoinMark },
            success: function () {
                if (this.status_code === 200) {
                    $('#BusinessModal').modal('hide');
                    plscommon.refreshTable("merchantuserinfo");
                } else {
                    plscommon.warningMessage(this.status_message, 3000);
                }
            }
        });
    }

    var btnDetailDialog = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('merchantuserinfo', 'DetailModal', 'MerchantDetail');
        if (!data.Id) {
            return false;
        }

        plscommon.ajax({
            url: defaults.detailUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                $("#detail_nickname").val(data.NickName);
                $("#detail_platformname").val(data.PlatFormName);
                $("#detail_name").val(data.Name);
                $("#detail_phone").val(data.Phone);
                $("#detail_notice").val(data.Notice);
                $("#detail_address").val(data.Address);
                $("#detail_businessstarthours").val(data.BusinessStartHours);
                $("#detail_businesssendhours").val(data.BusinessEndHours);
                $("#detail_distributionfee").val(data.DistributionFee);
                $("#detail_joinmark").val(returnJoinMark(data.JoinMark));
                $("#detail_createtime").val(data.CreateTime);
                $("#detail_logo").attr("src", data.Logo);
            }
        });
    }

    var returnJoinMark = function (value) {
        var result = "";
        if (value == 0) {
            result = "未营业";
        }
        if (value == 1) {
            result = "营业中";
        }
        return result;
    }

    var checkData = function (data) {
        if (!data.Name) {
            plscommon.warningMessage("商户名称不能为空，请您检查", 3000);
            return false;
        }
        if (data.PlatFormId == "-1") {
            plscommon.warningMessage("请您选择商户平台分类", 3000);
            return false;
        }
        if (!data.Notice || !data.Phone || !data.Address) {
            plscommon.warningMessage("商户公告、电话、地址不能为空，请您检查", 3000);
            return false;
        }
        if (!data.BusinessStartHours || !data.BusinessEndHours) {
            plscommon.warningMessage("商户营业开始结束时间不能为空，请您检查", 3000);
            return false;
        }
        if (!data.DistributionFee) {
            plscommon.warningMessage("商户配送费不能为空，请您检查", 3000);
            return false;
        }
        if (!data.Logo) {
            plscommon.warningMessage("商户Logo不能为空，请您检查", 3000);
            return false;
        }
        return true;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
            initDropDownList();     //初始化下拉事件
        }
    };
}());